<?php

namespace App\Libs\Redis;

use EasySwoole\Component\Singleton;

class Redis
{
    use Singleton;

    public $redis;

    private function __construct()
    {
        // 判断有无redis模块
        if (!extension_loaded('redis')) {
            throw new \Exception("redis模块未加载");
        }

        try {
            $this->redis = new \Redis();
//            $config = \EasySwoole\EasySwoole\Config::getInstance()->getConf('REDIS');
            $config = \Yaconf::get('redis');
            $res = $this->redis->connect($config['host'], $config['port'], $config['time_out']);
        } catch (\Exception $e) {
            throw new \Exception("redis服务异常");
        }

        if ($res === false) {
            throw new \Exception("redis连接失败");
        }
    }

    public function get($key)
    {
        if (empty($key)) {
            return '';
        }

        return $this->redis->get($key);
    }
}